//
// Created by changlei on 2022/4/29.
//

import Foundation
import UIKit


class UIButtonsBar: UIView {
    var items: [UIPlainTabBarItem] = [] {
        didSet {
            didItemsChanged()
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        onInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        onInit()
    }

    func onInit() {
    }

    func didItemsChanged() {
        subviews.forEach { view in
            view.removeFromSuperview()
        }
        for item in items {
            addSubview(item.button)
        }
        let subviews = subviews
        for (index, view) in subviews.enumerated() {
            view.snp.makeConstraints { make in
                make.top.height.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(1.0 / Double(subviews.count))
                if index == 0 {
                    make.leading.equalToSuperview()
                } else {
                    make.leading.equalTo(subviews[index - 1].snp.trailing)
                }
            }
        }
    }
}